package cn.kgc.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.kgc.entity.PmsBrand;
import cn.kgc.entity.PmsCategory;
import cn.kgc.service.PmsBrandService;
import cn.kgc.service.PmsCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.PmsCategoryBrandRelation;
import cn.kgc.dto.PmsCategoryBrandRelationAddDTO;
import cn.kgc.dto.PmsCategoryBrandRelationUpdateDTO;
import cn.kgc.service.PmsCategoryBrandRelationService;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.PmsCategoryBrandRelation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 品牌分类关联(PmsCategoryBrandRelation)表控制层
 *
 * @author makejava
 * @since 2022-11-29 12:08:17
 */
@RestController
@RequestMapping("pms/Category/BrandRelation")
@Api
@Validated
@Slf4j
public class PmsCategoryBrandRelationController {
    /**
     * 服务对象
     */
    @Resource
    private PmsCategoryBrandRelationService pmsCategoryBrandRelationService;
    @Resource
    private PmsCategoryService pmsCategoryService;
    @Resource
    private PmsBrandService pmsBrandService;



    /**
     * 根据商品的分类的ID查询品牌的名字
     */
    @GetMapping("/listByCategoryId")
    @ApiOperation(value = "根据分类的Id查询品牌", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @CrossOrigin
    @ApiImplicitParam(name = "categoryId",value = "分类的ID",paramType = "query",dataType = "long")
    public List<PmsBrand>selectPmsBrandByCategoryId(Long categoryId){
        //根据分类的ID查询分类对应的品牌信息
        LambdaQueryWrapper<PmsCategoryBrandRelation>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsCategoryBrandRelation::getCatelogId,categoryId);
        List<PmsCategoryBrandRelation> pmsCategoryBrandRelations =
                pmsCategoryBrandRelationService.list(lambdaQueryWrapper);

        //分类对应的品牌数据
        List<PmsBrand>pmsBrandList=new ArrayList<>();
        if(CollectionUtil.isEmpty(pmsCategoryBrandRelations)){
            return pmsBrandList;
        }
        for (PmsCategoryBrandRelation categoryBrandRelation : pmsCategoryBrandRelations) {
            Long brandId = categoryBrandRelation.getBrandId();
            PmsBrand pmsBrand = pmsBrandService.getById(brandId);
            pmsBrandList.add(pmsBrand);
        }
        return pmsBrandList;


    }
    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "brandId", value = "品牌的id", paramType = "query", dataType = "int"),
    })

    @CrossOrigin
    public IPage<PmsCategoryBrandRelation> selectAll(@RequestParam(defaultValue = "1") int current,
                                                     @RequestParam(defaultValue = "5") int pageSize,
                                                     @RequestParam Integer brandId) {
        log.info("查询{},对应的分类的信息", brandId);
        //根据品牌的ID，查询该品牌所关联的分类
        LambdaQueryWrapper<PmsCategoryBrandRelation>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsCategoryBrandRelation::getBrandId,brandId);
        IPage<PmsCategoryBrandRelation> page = new Page(current, pageSize);

        return pmsCategoryBrandRelationService.page(page,lambdaQueryWrapper);
    }



    /**
     * 新增数据
     *
     * @param pmsCategoryBrandRelationDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody PmsCategoryBrandRelationAddDTO pmsCategoryBrandRelationDTO) {

        //新增保证数据库中分类与品牌的ID唯一，方案1 可以在数据库中创建一个唯一的组合索引
        //方案2，数据库中根据分类的ID和品牌的ID查询，关系是否存在，如果存在，返回错误信息
        //todo ....


        //品牌以及分类的信息，需要从数据库中查询出来，然后设置到关系表中
        Long categoryId = pmsCategoryBrandRelationDTO.getCatelogId();
        Long brandId = pmsCategoryBrandRelationDTO.getBrandId();

        PmsBrand pmsBrand = pmsBrandService.getById(brandId);
        PmsCategory pmsCategory = pmsCategoryService.getById(categoryId);


        //封装要新增的品牌与分类的信息
        PmsCategoryBrandRelation pmsCategoryBrandRelation = new PmsCategoryBrandRelation();
        BeanUtils.copyProperties(pmsCategoryBrandRelationDTO, pmsCategoryBrandRelation);

        pmsCategoryBrandRelation.setCatelogName(pmsCategory.getName());
        pmsCategoryBrandRelation.setBrandName(pmsBrand.getName());
        return this.pmsCategoryBrandRelationService.save(pmsCategoryBrandRelation);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.pmsCategoryBrandRelationService.removeById(id);
    }
}
